<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务日期" prop="taskDate">
        <el-input
                v-model="queryParams.taskDate"
                placeholder="请输入任务日期"
                clearable
                @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务时间" prop="taskTime">
        <el-input
                v-model="queryParams.taskTime"
                placeholder="请输入任务时间"
                clearable
                @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="taskCreator">
        <el-input
                v-model="queryParams.taskCreator"
                placeholder="请输入创建人"
                clearable
                @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="群id" prop="roomid">
        <el-input
                v-model="queryParams.roomid"
                placeholder="请输入群id"
                clearable
                @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消息时间" style="width: 308px">
        <el-date-picker
                v-model="daterangeMsgtime"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="原始消息" prop="orignalContent">
        <el-input
                v-model="queryParams.orignalContent"
                placeholder="请输入原始消息"
                clearable
                @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地点" prop="taskAddress">
        <el-input
                v-model="queryParams.taskAddress"
                placeholder="请输入地点"
                clearable
                @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有问题" prop="taskIsProblem">
        <el-input
                v-model="queryParams.taskIsProblem"
                placeholder="请输入是否有问题"
                clearable
                @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有结论" prop="taskIsResult">
        <el-input
                v-model="queryParams.taskIsResult"
                placeholder="请输入是否有结论"
                clearable
                @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有解决方案" prop="taskIsSolution">
        <el-input
                v-model="queryParams.taskIsSolution"
                placeholder="请输入是否有解决方案"
                clearable
                @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-if="false"
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
                v-hasPermi="['ai:tasks:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="success"
                plain
                icon="Edit"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['ai:tasks:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="danger"
                plain
                icon="Delete"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['ai:tasks:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
                type="warning"
                plain
                icon="Download"
                @click="handleExport"
                v-hasPermi="['ai:tasks:export']"
        >导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
                type="primary"
                plain
                icon="cpu"
                @click="handleReAITask"
                v-hasPermi="['ai:tasks:export']"
        >指定日期AI重新总结</el-button>
      </el-col>

      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tasksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="归纳一句话" align="center" prop="taskMemo" />
      <el-table-column label="行动项" align="left"  >
        <template #default="scope">
          <div style="overflow: hidden; height: 70px;width: 150px; white-space: nowrap;text-overflow: ellipsis;">
            <span  :title="splitAct(scope.row.taskDetail)" >{{splitAct(scope.row.taskDetail)}}</span><!-- prop="encryptChatMsg"-->
          </div>
        </template>
      </el-table-column>
      <el-table-column label="任务日期" align="center" prop="taskDate" />
      <el-table-column label="任务时间" align="center" prop="taskTime" />
      <el-table-column label="创建人" align="center" prop="taskCreator" />
      <el-table-column label="参与人列表(不含创建人)" align="center" prop="taskUserlist" />
      <el-table-column label="关键词" align="center" prop="taskKeyword" />
      <el-table-column label="任务解析原始Json" align="center" prop="taskOrignalJson" v-if="false" />
      <el-table-column label="群id" align="center" prop="roomid" />
      <el-table-column label="消息时间" align="center" prop="msgtime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.msgtime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原始消息" align="left"  >
        <template #default="scope">
          <div style="overflow: hidden; height: 30px;width: 100px; white-space: nowrap;text-overflow: ellipsis;">
            <span  :title="scope.row.orignalContent" >{{scope.row.orignalContent}}</span><!-- prop="encryptChatMsg"-->
          </div>
        </template>
      </el-table-column>
      <el-table-column label="原始数据" align="center" prop="orignalMsg"  v-if="false"/>
      <el-table-column label="是否会议  0否 1是" align="center" prop="isMeeting"  v-if="false"/>
      <el-table-column label="内容类型" align="center" prop="taskActions"/>
      <el-table-column label="地点" align="center" prop="taskAddress" />
      <el-table-column label="是否有问题" align="center" prop="taskIsProblem" />
      <el-table-column label="问题" align="center" prop="taskProblem" />
      <el-table-column label="是否有结论" align="center" prop="taskIsResult" />
      <el-table-column label="结论" align="center" prop="taskResult" />
      <el-table-column label="是否有解决方案" align="center" prop="taskIsSolution" />
      <el-table-column label="解决方案" align="center" prop="taskSolution" />
      <el-table-column label="资源" align="center" prop="taskRes" />
      <el-table-column label="术语" align="center" prop="taskTerms" />
      <el-table-column label="指标" align="center" prop="taskTargets" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ai:tasks:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ai:tasks:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
            v-show="total>0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
    />

    <!-- 添加或修改AI任务对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="tasksRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务简介" prop="taskMemo">
          <el-input v-model="form.taskMemo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="任务详情" prop="taskDetail">
          <el-input v-model="form.taskDetail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="任务日期" prop="taskDate">
          <el-input v-model="form.taskDate" placeholder="请输入任务日期" />
        </el-form-item>
        <el-form-item label="任务时间" prop="taskTime">
          <el-input v-model="form.taskTime" placeholder="请输入任务时间" />
        </el-form-item>
        <el-form-item label="创建人" prop="taskCreator">
          <el-input v-model="form.taskCreator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="参与人列表(不含创建人)" prop="taskUserlist">
          <el-input v-model="form.taskUserlist" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="关键词" prop="taskKeyword">
          <el-input v-model="form.taskKeyword" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="任务解析原始Json" prop="taskOrignalJson">
          <el-input v-model="form.taskOrignalJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="群id" prop="roomid">
          <el-input v-model="form.roomid" placeholder="请输入群id" />
        </el-form-item>
        <el-form-item label="消息时间" prop="msgtime">
          <el-date-picker clearable
                          v-model="form.msgtime"
                          type="date"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择消息时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="原始消息" prop="orignalContent">
          <el-input v-model="form.orignalContent" placeholder="请输入原始消息" />
        </el-form-item>
        <el-form-item label="原始数据" prop="orignalMsg">
          <el-input v-model="form.orignalMsg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否会议  0否 1是" prop="isMeeting">
          <el-input v-model="form.isMeeting" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="行动项" prop="taskActions">
          <el-input v-model="form.taskActions" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="地点" prop="taskAddress">
          <el-input v-model="form.taskAddress" placeholder="请输入地点" />
        </el-form-item>
        <el-form-item label="是否有问题" prop="taskIsProblem">
          <el-input v-model="form.taskIsProblem" placeholder="请输入是否有问题" />
        </el-form-item>
        <el-form-item label="问题" prop="taskProblem">
          <el-input v-model="form.taskProblem" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否有结论" prop="taskIsResult">
          <el-input v-model="form.taskIsResult" placeholder="请输入是否有结论" />
        </el-form-item>
        <el-form-item label="结论" prop="taskResult">
          <el-input v-model="form.taskResult" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否有解决方案" prop="taskIsSolution">
          <el-input v-model="form.taskIsSolution" placeholder="请输入是否有解决方案" />
        </el-form-item>
        <el-form-item label="解决方案" prop="taskSolution">
          <el-input v-model="form.taskSolution" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="资源" prop="taskRes">
          <el-input v-model="form.taskRes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="术语" prop="taskTerms">
          <el-input v-model="form.taskTerms" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="指标" prop="taskTargets">
          <el-input v-model="form.taskTargets" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Tasks">
  import { listTasks, getTasks, delTasks, addTasks, updateTasks, reTask } from "@/api/ai/tasks";

  const { proxy } = getCurrentInstance();

  const tasksList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  const daterangeMsgtime = ref([]);

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      taskMemo: null,
      taskDetail: null,
      taskDate: null,
      taskTime: null,
      taskCreator: null,
      taskUserlist: null,
      taskKeyword: null,
      taskOrignalJson: null,
      roomid: null,
      msgtime: null,
      orignalContent: null,
      orignalMsg: null,
      isMeeting: null,
      taskActions: null,
      taskAddress: null,
      taskIsProblem: null,
      taskProblem: null,
      taskIsResult: null,
      taskResult: null,
      taskIsSolution: null,
      taskSolution: null,
      taskRes: null,
      taskTerms: null,
      taskTargets: null
    },
    rules: {
    }
  });

  const { queryParams, form, rules } = toRefs(data);

  /** 查询AI任务列表 */
  function getList() {
    loading.value = true;
    queryParams.value.params = {};
    if (null != daterangeMsgtime && '' != daterangeMsgtime) {
      queryParams.value.params["beginMsgtime"] = daterangeMsgtime.value[0];
      queryParams.value.params["endMsgtime"] = daterangeMsgtime.value[1];
    }
    listTasks(queryParams.value).then(response => {
      tasksList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }

  function splitAct(actions){
    if(actions==null){
      return "";
    }

    var str = "";
    //var jsonObj = JSON.parse("[\"1. 先不要进行更新。\",\"2. 下周一白天一起更新。\",\"3. 以后尽可能在白天进行更新。\"]");
    try{
      var jsonObj = JSON.parse(actions);
      //var jsonObj = JSON.parse("[{\"行动项\":\"王昊Will进行测试\"}]");

      console.log(jsonObj)
      for (const obj in jsonObj) {

        if(typeof jsonObj[obj] === "object"){
          try{
            var tmpJson = jsonObj[obj];
            for (let key in tmpJson) {
              str = str+"◆"+tmpJson[key]+"\n";
            }
          }catch (e) {
            console.log(e);
            str = str+"◆"+jsonObj[obj]+"\n";
          }
        }else{
          str = str+"◆"+jsonObj[obj]+"\n";
        }
      }
    }catch (e) {
      str = actions;
    }

    return str;
  }

  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }

  // 表单重置
  function reset() {
    form.value = {
      id: null,
      taskMemo: null,
      taskDetail: null,
      taskDate: null,
      taskTime: null,
      taskCreator: null,
      taskUserlist: null,
      taskKeyword: null,
      taskOrignalJson: null,
      roomid: null,
      msgtime: null,
      orignalContent: null,
      orignalMsg: null,
      isMeeting: null,
      taskActions: null,
      taskAddress: null,
      taskIsProblem: null,
      taskProblem: null,
      taskIsResult: null,
      taskResult: null,
      taskIsSolution: null,
      taskSolution: null,
      taskRes: null,
      taskTerms: null,
      taskTargets: null
    };
    proxy.resetForm("tasksRef");
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    daterangeMsgtime.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
  }

  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加AI任务";
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getTasks(_id).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改AI任务";
    });
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["tasksRef"].validate(valid => {
      if (valid) {
        if (form.value.id != null) {
          updateTasks(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addTasks(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除AI任务编号为"' + _ids + '"的数据项？').then(function() {
      return delTasks(_ids);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('ai/tasks/export', {
      ...queryParams.value
    }, `tasks_${new Date().getTime()}.xlsx`)
  }

  /** AI重新输入 **/
  function handleReAITask() {

    var userInput = prompt("请输入需要重新总结的日期:", "2024-08-01");

    // 检查用户是否点击了取消或输入了内容
    if (userInput != null) {
      // 如果用户没有点击取消，则输入的内容不为null
      //alert("你好，" + userInput);
      var data = {
        taskDate: userInput
      };
      reTask(data).then(response => {
        proxy.$modal.msgSuccess("提交成功");
      });

    } else {
      // 如果用户点击了取消，则输入的内容为null
      alert("你没有输入任何内容。");
    }

  }

  getList();
</script>
